from typing import Dict, Any
from myProfile.models.data.myProfile_models import Login, Profile


class ProfileRepository:

    def __init__(self, engine):
        self.engine = engine

    async def insert_profile(self, id: int, details: Dict[str, Any]) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == id)
            login.profiles = Profile(**details)
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def update_profile(self, id: int, details: Dict[str, Any]) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == id)
            profile = login.profiles
            for key, value in details.items():
                setattr(profile, key, value)
            login.profiles = profile
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def delete_profile(self, id: int) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == id)
            login.profiles = Profile()
            await self.engine.save(login)
        except Exception:
            return False
        return True

    async def get_profile(self, id: int) -> Profile:
        login = await self.engine.find_one(Login, Login.id == id)
        return login.profiles
